	import dm5 from "./md5.js";
export function setsign(usetToken,query,body){
	// 获取预先设置为环境变量的 APPKEY
	let user_app_key = '2a281708a0034b3c';// pm.environment.get('APPKEY')
	let token = usetToken;
	// 存放所有需要用来签名的参数
	let param = {}
	
	// 加入 query 参数
	let queryParams = query
	if (queryParams) {
		for (let key in queryParams) {
		    let value = queryParams[key]
			if (value !== '' && (typeof value == 'string' || typeof value == 'number')){
				if(typeof value == 'string'){
					value = value.trim();
				}
		        // 非空参数值的参数才参与签名
		        param[key] = value
		    }
		}
	}


	// 加入 body 参数
	if (body) {
	    let formData = body
		for (let key in formData) {
			let value = formData[key] // 此处要注意如果值的实际类型不是 string 需要根据实际情况处理。
			if (value !== '' && (typeof value == 'string' || typeof value == 'number')){
				if(typeof value == 'string'){
					value = value.trim();
				}
				// 非空参数值的参数才参与签名
				param[key] = value
			}
		}
	}
	delete param['sign'];
	delete param['token'];
	delete param['_csrf'];
	delete param['callback'];
	// 取 key
	let keys = []
	for (let key in param) {
	    keys.push(key)
	}
	// 参数名 ASCII 码从小到大排序（字典序）
	keys.sort()
	
	// 转成键值对
	let paramPair = []
	for (let i = 0, len = keys.length; i < len; i++) {
	    let k = keys[i]
	    paramPair.push(k + '=' + encodeURIComponent(param[k])) // urlencode 编码
	}
	// 拼接
	let stringSign = paramPair.join('&').toUpperCase()
	stringSign = stringSign + '&' + user_app_key
	stringSign += token ? '&'+token : '';
	let sign = dm5(stringSign)
	/* console.log("sign",sign);
	console.log('stringSign',stringSign); */
	// 增加 header 参数
	/* pm.request.headers.add({
	    key: "sign",
	    value: sign,
	});
	pm.environment.set("sign", sign); */
	return sign
}